package com.huawei.classroom.student.h15;

import java.util.ArrayList;
import java.util.List;

public class PrimeUtil {
	public List<Long> getPrimeList(long start, long end, int threadCount){
		List<Long> list = new ArrayList<>();//不能用一个static的List来一次装每个线程得出的质数，会乱序且有null存在。。。
		NewThread[] newthread = new NewThread[threadCount]; 
		for(int i=0; i<newthread.length; i++) {//用线程分头找质数
			long threadstart = start + (end - start) / threadCount * i;
			long threadend = start + (end - start) / threadCount * (i+1);
			newthread[i] = new NewThread(threadstart, threadend);
			newthread[i].start();
		}
		for(int i=0; i<newthread.length; i++) {
			try {
				newthread[i].join();
				list.addAll(newthread[i].getlist());//每次把一个装进来
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
